﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.ResourceManagement.Client;
using Microsoft.ResourceManagement.ObjectModel;
using Microsoft.ResourceManagement.ObjectModel.ResourceTypes;

namespace examples {
    class Program {
        static void Main(string[] args) {

            using (DefaultClient client = new DefaultClient()) {
                // set credentials and refresh schema
                client.ClientCredential = new System.Net.NetworkCredential("user", "pwd", "domain");
                client.RefreshSchema();

                // Enumerate the approvals and approve specifying an SPN.
                foreach (RmResource resource in client.Enumerate(string.Format("/Approval[ApprovalStatus='Pending']"))) {
                    RmApproval approval = resource as RmApproval;
                    if (approval != null) {
                        EndpointAddress address = new EndpointAddress(
                            new Uri(approval.ApprovalEndpointAddress),
                            EndpointIdentity.CreateSpnIdentity("FIMService/FIMMachine"));
                        Console.WriteLine("Approving request {0} on endpoint {1}: ", approval.ObjectID, approval.ApprovalEndpointAddress);
                        try {
                            client.Approve(approval, true, address);
                            Console.WriteLine("Success.");
                        } catch (Exception exc) {
                            Console.WriteLine("Failed: {0}", exc);
                        }
                    }
                }
            }
        }
    }
}
